package com.app.extended_plugin.utlis;

import android.graphics.Rect;
import android.util.Log;
import android.view.View;
import android.view.ViewTreeObserver;


public class KeyBoardUtils {

    /** * 判断软键盘是否弹出
     * * @param rootView
     * @param listener
     *备注：在不用的时候记得移除OnGlobalLayoutListener
     */
    public static ViewTreeObserver.OnGlobalLayoutListener doMonitorSoftKeyWord(final View rootView, final OnSoftKeyWordShowListener listener) {
        final ViewTreeObserver.OnGlobalLayoutListener layoutListener = () -> {
            final Rect rect = new Rect();
            rootView.getWindowVisibleDisplayFrame(rect);
            boolean visible = ((double)rect.height() / (double)rootView.getRootView().getHeight()) < 0.85;
            if (listener != null)
                listener.hasShow(visible);
        };
        rootView.getViewTreeObserver().addOnGlobalLayoutListener(layoutListener);
        return layoutListener;
    }

    public static void removeKeyWordListener(final View rootView,final ViewTreeObserver.OnGlobalLayoutListener listener){
        rootView.getViewTreeObserver().removeOnGlobalLayoutListener(listener);
    }
    public interface OnSoftKeyWordShowListener {
        void hasShow(boolean isShow);

    }
}
